local Player = FindMetaTable("Player")

local function reloadPlayerClass( ply )
	local ClassId = Class.GetPlayerClassId( ply )
	
	if ply._NCClass && ply._NCClass.Id == ClassId then
		return
	end
	
	if !Class.Exists( ClassId ) then
		ClassId = Class.SCOUT
	end	

	ply._NCClass = Class.New( ply, ClassId )
end

function Player:Class()

	if !self._NCClass  then
		reloadPlayerClass( self )
	end

	return self._NCClass 
end

function Player:SQLId()
	if !self.SteamID then
		debug.traceback()
		Error("Trying to get player steamid before player is created!")
	end
		
	local SteamId = self:SteamID()
	local Findings = {}
		
	for w in string.gmatch( SteamId , "%d+") do
		table.insert( Findings, w )
	end
		
	if #Findings == 3 then
		local Id = (tonumber(Findings[3]) * 2) + tonumber(Findings[2])
		self._GTSqlID = Id
		
		self.SQLId = function()
			return Id
		end
		return Id
	else
		if SteamId != "STEAM_ID_PENDING" && SteamId != "UNKNOWN" then
				SQLLog( 'error', "sql id could not be found (".. tostring(SteamId) ..")\n" )
		end
		return 
	end

end



function GM:PlayerLoadout( ply )

	if Class.DEBUG then
		print("Loadout for: ", ply, "(" .. tostring(Game.StateName()) .. ")" )
	end
	
	reloadPlayerClass( ply )
	
	local class = ply:Class()
	
	if Game.State() == Game.BUILD then
	
		ply:Give("weapon_physcannon")
	
	else
		
		local class = ply:Class()
		
		ply:RemoveAllAmmo() //Any left over from state change?
		
		class:Spawn()
		class:SetAmmo()
		
	end
	
end

function GM:PlayerSetModel( ply )
	
	//If the team could not set the player's model, then let the base class handle it
	if !Team.UpdateModel( ply ) then
		self.BaseClass.PlayerSetModel( self, ply )
	end	
	
end